package com.sdx.console.live.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.live.entity.CourseLiveAppointment;
import com.sdx.console.live.service.CourseLiveAppointmentService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 直播预约 接口
 */
@RequestMapping("/live/liveAppointment")
@Controller
public class CourseLiveAppointmentController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(CourseLiveAppointmentController.class);
    private static final String MODEL = "/live/liveAppointment";


    @Resource
	private CourseLiveAppointmentService liveAppointmentService;

	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("live.liveAppointment.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request,Integer liveId) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
//        CourseLiveAppointment courseLiveAppointment = new CourseLiveAppointment();
//        courseLiveAppointment.setLiveId(liveId);
//        pChapterIdList =  liveAppointmentService.template();

        view.addObject("liveId",liveId);

        return view;
    }

    @GetMapping("/edit.do")
    @Function("live.liveAppointment.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer appintmentId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        CourseLiveAppointment liveAppointment = liveAppointmentService.queryById(appintmentId);
        view.addObject("liveAppointment", liveAppointment);
		setPageParam(view,request);
		try {
			view.addObject("liveAppointmentJson", JacksonUtils.toJsonString(liveAppointment,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("live.liveAppointment.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
		setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("live.liveAppointment.query")
    @ResponseBody
    public JsonResult<PageQuery> list(CourseLiveAppointment condtion)
    {
        PageQuery page = condtion.getPageQuery();
        if(condtion.getLiveId()!=null) {
            liveAppointmentService.queryByCondition(page);
        }
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,CourseLiveAppointment liveAppointment,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
//			liveAppointment.setCreatedBy(user.getId().intValue());
//			liveAppointment.setCreatedName(user.getName());
//			liveAppointment.setCreatedTime(new Date());
//			liveAppointment.setUpdatedBy(user.getId().intValue());
//			liveAppointment.setUpdatedName(user.getName());
//			liveAppointment.setUpdatedTime(new Date());
			liveAppointmentService.save(liveAppointment);
		} else {
//			liveAppointment.setUpdatedBy(user.getId().intValue());
//			liveAppointment.setUpdatedName(user.getName());
//			liveAppointment.setUpdatedTime(new Date());
			liveAppointmentService.updateTemplate(liveAppointment);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("live.liveAppointment.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)CourseLiveAppointment liveAppointment,HttpServletRequest request
		)
    {

        return save(true,liveAppointment,request
		);
    }

    @PostMapping("/update.json")
    @Function("live.liveAppointment.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  CourseLiveAppointment liveAppointment,HttpServletRequest request
		) {
		return save(false,liveAppointment,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("live.liveAppointment.query")
    @ResponseBody
    public JsonResult<CourseLiveAppointment>queryInfo(Integer appintmentId) {
        CourseLiveAppointment liveAppointment = liveAppointmentService.queryById( appintmentId);
        return  JsonResult.success(liveAppointment);
    }

    @PostMapping("/delete.json")
    @Function("live.liveAppointment.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        liveAppointmentService.batchDelCourseLiveAppointment(idList);
        return JsonResult.success();
    }
    

}
